﻿using KPI.Api;
using KPI.Api.Apis;
using KPI.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_TY_UI;

namespace KPIClient.Views
{
    /// <summary>
    /// ViewEditQuestionNaire.xaml 的交互逻辑
    /// </summary>
    public partial class ViewEditQuestionNaire : TYUserControl
    {
        ViewEditQuestionNaireModel vm = new ViewEditQuestionNaireModel();
        public string questionNaireId { get; set; }

        public ViewEditQuestionNaire()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewEditQuestionNaire_Loaded;
        }

        async Task<bool> QueryData()
        {
            QuestionBankQueryDto queryDto = new QuestionBankQueryDto();
            queryDto.page = pager.PageIndex;
            queryDto.limit = pager.PageSize;
            queryDto.questionName = vm.SearchQtName;
            if (vm.SelectedQtType != null && vm.SelectedQtType.Key > 0)
            {
                queryDto.queryType = vm.SelectedQtType.Key;
            }

            vm.ListQuestionBank.Clear();
            var data = await Run(() => QuestionBankApi.GetQuestionBankList(queryDto));
            if (data == null || data.code != 0)
            {
                return false;
            }
            foreach (var item in data.data?.list)
            {
                QuestionInfoModel model = new QuestionInfoModel();
                model.FromData(item);
                model.Index = vm.ListQuestionBank.Count + 1;
                vm.ListQuestionBank.Add(model);
            }
            pager.DataCount = data.data.total;
            pager.PageCount = data.data.pages;
            return true;
        }

        private async void ViewEditQuestionNaire_Loaded(object sender, RoutedEventArgs e)
        {
            vm.ListQuestionInExam.Clear();
            var rData = await Run(() => QuesionNaireApi.GetQuestionNareInfo(questionNaireId));
            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            if (rData.data != null)
            {
                if (rData.data.listQbId == null)
                    rData.data.listQbId = new List<string>();
                vm.FromData(rData.data);

            }

            BtnClick_Search(null, null);

        }

        private async void pager_PageIndexChanged(object sender, RoutedEventArgs e)
        {
            await QueryData();
        }

        private async void BtnClick_AddToQuesionNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx != null)
            {
                var qbId = dx.Data.qbId;
                var rData = await Run(() => QuestionBankApi.GetQuestionAllInfo(qbId));
                if (rData == null)
                {
                    MessageBox.Show("数据异常,请检查日志!");
                    return;
                }
                if (rData.code != 0)
                {
                    MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                    return;
                }
                if (rData.data != null)
                {
                    var model = new QuestionInfoModel();
                    model.FromData(rData.data);
                    model.Index = vm.ListQuestionInExam.Count + 1;
                    vm.ListQuestionInExam.Add(model);
                }

            }
        }

        private async void BtnClick_EndEdit(object sender, RoutedEventArgs e)
        {
            var data = vm.Data;
            data.listQbId.Clear();
            foreach (var item in vm.ListQuestionInExam)
            {
                data.listQbId.Add(item.Data.qbId);
            }
            var rData = await Run(() => QuesionNaireApi.UpdateQuestionNareInfo(data));
            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            MessageBox.Show("已完成编辑!");
        }

        private void BtnClick_Preview(object sender, RoutedEventArgs e)
        {
            ViewPreviewNaire view = new ViewPreviewNaire(vm);
            view.ShowDialog();
        }

        private void ResetIndex()
        {
            for (int i = 0; i < vm.ListQuestionInExam.Count; i++)
            {
                vm.ListQuestionInExam[i].Index = i + 1;
            }
        }

        private void BtnClick_Delete_Qt_InNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx == null)
                return;
            vm.ListQuestionInExam.Remove(dx);
            ResetIndex();

        }

        private void BtnClick_Up_Qt_InNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx == null)
                return;
            var index = vm.ListQuestionInExam.IndexOf(dx);
            if (index - 1 > 0)
            {
                vm.ListQuestionInExam.Remove(dx);
                vm.ListQuestionInExam.Insert(index - 1, dx);
                ResetIndex();
            }

        }

        private void BtnClick_Down_Qt_InNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx == null)
                return;
            var index = vm.ListQuestionInExam.IndexOf(dx);
            if (index + 1 < vm.ListQuestionInExam.Count)
            {
                vm.ListQuestionInExam.Remove(dx);
                vm.ListQuestionInExam.Insert(index + 1, dx);
                ResetIndex();
            }
        }

        private void BtnClick_Top_Qt_InNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx == null)
                return;

            vm.ListQuestionInExam.Remove(dx);
            vm.ListQuestionInExam.Insert(0, dx);
            ResetIndex();
        }

        private void BtnClick_Bottom_Qt_InNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx == null)
                return;

            vm.ListQuestionInExam.Remove(dx);
            vm.ListQuestionInExam.Add(dx);
            ResetIndex();
        }

        private async void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            pager.PageIndex = 1;
            await QueryData();
        }
    }
}
